<?php
header('Content-Type:text/html; Charset=UTF-8');
include 'lib/checker.php';

@$pid = $_REQUEST['pid'];

$title = '编辑联系人错误提示';
$jumpUrl = 'listperson.php';

if(empty($pid)){
	$message = '获取联系人信息出错';
	include 'lib/message.php';
	die;
}

require 'lib/db.php';

@$person = dbSelect('select name,imgUrl from person where id=:id',array('id'=>$pid));
@$numbers = dbSelectAll('select id,type,content from number where pid=:pid',array('pid'=>$pid));
// var_dump($person);
// var_dump($numbers);

$mobileExist = false;
$qqExist = false;
$homeExist = false;
$officeExist = false;

foreach ($numbers as $number){
	if($number['type']=='手机'){
		$mobileExist = true;
	}elseif($number['type']=='QQ'){
		$qqExist = true;
	}elseif($number['type']=='家庭'){
		$homeExist = true;
	}elseif($number['type']=='办公'){
		$officeExist = true;
	}
}

include 'lib/header.php';
?>

				<div class="row">
	                <div class="col-xs-12">
	                    <div class="panel panel-info">
	                        <div class="panel-heading">
	                        	<div class="row">
	                            	<h1 class="panel-title">编辑【<?php echo $person['name'];?>】</h1>
	                        	</div>
	                        </div>
	                            <form class="form-horizontal" action="doEditperson.php" method="post" enctype="multipart/form-data" >
	                                <input type="hidden" name="pid" value="<?php echo $pid;?>"/>
	                                <div class="panel-body">
	                                	<div class="form-group text-center">
	                                		<img src="imgs/<?php echo $person['imgUrl'];?>" width="160" height="160" >
	                                	</div>
	                                	<div class="form-group">
	                                	<label class="control-label col-xs-3">照片：</label>
											<div class="col-xs-8 ">
											<label class="sr-only" for="1113"></label>
										    <div class="input-group">
										      <input name="img" type="file" class="filestyle" data-buttonBefore="true" data-buttonText="选择文件" /><br />
										    </div>
											</div>
	                                	</div>
										<div class="form-group">
	                                	<label class="control-label col-xs-3">姓名：</label>
											<div class="col-xs-8 ">
											<input id="nameInput" value="<?php echo $person['name']; ?>" class="form-control" name="newname" placeholder="姓名"/>
				      						<input type="hidden" value="<?php echo $person['name']; ?>" class="form-control" name="oldname" />
											</div>
	                                	</div>
	                                	<?php foreach ($numbers as $number){?>
										<div class="form-group">
	                                	<label class="control-label col-xs-3"><?php echo $number['type']?>：</label>
											<div class="col-xs-8 ">
											<label class="sr-only" for="1113"></label>
										    <div class="input-group">
										    <?php if($number['type']=='手机'){?>
										    	<input type="text" class="form-control" name="newmobiletext" value="<?php echo $number['content']?>">
										    	<input name="oldmobiletext" type="hidden" value="<?php echo $number['content'];?>" class="form-control"  />	
										    <?php }elseif($number['type']=='QQ'){ ?>
										    	<input type="text" class="form-control" name="newQQtext" id="1113" value="<?php echo $number['content']?>">
										    	<input  class="form-control" type="hidden" name="oldQQtext" id="1113" value="<?php echo $number['content']?>">
										    <?php }elseif($number['type']=='家庭'){ ?>
										    	<input type="text" class="form-control" name="newhometext" id="1113" value="<?php echo $number['content']?>">
										    	<input  class="form-control" type="hidden" name="oldhometext" id="1113" value="<?php echo $number['content']?>">
										    <?php }elseif($number['type']=='办公'){ ?>
										    	<input type="text" class="form-control" name="newofficetext" id="1113" value="<?php echo $number['content']?>">
										    	<input  class="form-control" type="hidden" name="oldofficetext" id="1113" value="<?php echo $number['content']?>">
										    <?php } ?>
										      
										      <div class="input-group-addon" class="btn btn-default" data-toggle="modal" data-target="#delete<?php echo $number['id'];?>">删除</div>
										    </div>
											</div>
	                                	</div>
	                                	<?php } ?>
			                                	<?php if (count($numbers)!=4){?>
			                                		<div class="form-group">
				                                	<label class="control-label col-xs-3">添加：</label>
														<div class="col-xs-8 ">
															<a type="submit" class="btn btn-default" data-toggle="modal" data-target="#ww"><span class="glyphicon glyphicon-plus"></span></a>
														</div>
				                                	</div>
			                                	<?php } ?>
										<div class="col-xs-3"></div>
											<button type="submit" class="btn btn-default">修改</button>
											<a href="listperson.php" class="btn btn-default">取消</a>
	                        		</div>
	                        		<div class="panel-footer">
	                            		<a class="btn btn-danger btn-lg btn-block" data-toggle="modal" data-target="#deleteperson">删除用户</a>
	                        		</div>
	                            </form>
	                    </div>
	                </div>
	            </div>
	            	<div id="deleteperson" class="modal">
                    	<div class="container">
                    		<div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <a class="close" data-dismiss="modal">&times;</a>
                                        <h2>删除确认</h2>
                                    </div>
                                    <form class="form-horizontal" action="doDeletePerson.php" method="post" >
                                    <div class="modal-body">
                                        <h4>确定删除 用户：<?php echo $person['name'];?></h4>
                                        <input type="hidden" name="pid" value="<?php echo $pid;?>"/>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="submit" class="btn btn-danger btn-lg">确定</button>
                                        <button data-dismiss="modal" class="btn btn-success btn-lg">取消</button>
                                    </div>
                                    </form>
                                </div>
                            </div>
                    	</div>
                    </div>
                    <?php foreach ($numbers as $number){?>
                    	<div id="delete<?php echo $number['id'];?>" class="modal">
	                    	<div class="container">
	                    		<div class="modal-dialog">
	                                <div class="modal-content">
	                                    <div class="modal-header">
	                                        <a class="close" data-dismiss="modal">&times;</a>
	                                        <h2>删除确认</h2>
	                                    </div>
	                                    <div class="modal-body">
	                                        <h4>确定删除 <?php echo $number['type'];?>号码<?php echo $number['content']?>？</h4>
	                                        <input type="hidden" name="id" value="<?php echo $pid;?>"/>
	                                    </div>
	                                    <div class="modal-footer">
	                                        <a href="doDeleteNumber.php?nid=<?php echo $number['id']?>&pid=<?php echo $pid;?>" class="btn btn-danger btn-lg">确定</a>
	                                        <button data-dismiss="modal" class="btn btn-success btn-lg">取消</button>
	                                    </div>
	                                </div>
	                            </div>
	                    	</div>
	                    </div>
	            	<?php } ?>
			                    <div id="ww" class="modal">
			                        <div class="container">
			                            <div class="modal-dialog">
			                                <div class="modal-content">
			                                    <div class="modal-header">
			                                        <a class="close" data-dismiss="modal">&times;</a>
			                                        <h1>添加条目</h1>
			                                    </div>
			                                    <form action="doAddNumber.php" method="post" >
                                                <input type="hidden" name="pid" value="<?php echo $pid;?>"/>
			                                    <div class="modal-body">
			                                        <div class="form-horizontal">
														<div class="form-group">
														<label class="col-xs-3 hidden-xs control-label" for="typeInput">类型:</label>
														<div class="col-xs-8">
															<select id="typeInput" name="type" class="form-control">
																<option value="请选择">请选择</option>
																<?php if(!$mobileExist){?>
																	<option value="手机">手机</option>
																<?php }?>
																<?php if(!$qqExist){?>
																	<option value="QQ">QQ</option>
																<?php }?>
																<?php if(!$officeExist){?>
																	<option value="办公">办公</option>
																<?php }?>
																<?php if(!$homeExist){?>
																	<option value="家庭">家庭</option>
																<?php }?>
															</select>
														</div>
					                                	</div>
			                                            <div class="form-group">
			                                                <label class="col-xs-3 control-label">内容：</label>
			                                                <div class="col-xs-8">
			                                                    <input class="form-control" name="content" />
			                                                </div>
			                                            </div>
			                                        </div>
			                                    </div>
			                                    <div class="modal-footer">
			                                        <button type="submit" class="btn btn-default">确定</button>
			                                        <button class="btn btn-default" data-dismiss="modal">返回</button>
			                                    </div>
		                                    </form>
		                                </div>
		                            </div>
		                        </div>
		                    </div>
<?php include 'lib/footer.php';?>